use "actions T1 - T6.dta", clear

keep if treatment==4|treatment==3

expand 91
sort session Subject Period
by session Subject Period: gen TimeStamp=_n
replace TimeStamp=TimeStamp-1

sort session Subject Period TimeStamp

drop if TimeStamp>50&(session == "150520_1335"|session == "150520_0847"|session=="150519_1349")

keep session Period Subject TimeStamp NetworkGroup MatchingGroup     LinkedSubjects1     LinkedSubjects2     LinkedSubjects3     LinkedSubjects4
sort session Period Subject TimeStamp

save pippo, replace


use "messages bare communication.dta", clear


***we drop observations were more message C were sent in the same value of TimaStamp

sort session Period Subject TimeStamp seq

by session Period Subject TimeStamp: keep if _n==_N

joinby session Period Subject TimeStamp using pippo, unmatched(both)


**************************
**************************
**************************

gen message=0
replace message=1 if Message=="C"
  
 
 local i=1
 while `i'<33 {
 gen mess`i'=message if Subject==`i'
 local i=`i'+1
 }
 

 local i=1
 while `i'<33 {

 sort session Period TimeStamp
 by session Period TimeStamp: egen mm`i'=total(mess`i')
 
 local i=`i'+1
 }
 
 local i=1
 while `i'<33 {

 replace mess`i'= mm`i'
 
 local i=`i'+1
 }
 
 
 drop mm1-mm32
 
 
sort session MatchingGroup NetworkGroup
egen NetGroup=group(session MatchingGroup NetworkGroup)



local i=1
while `i'<33 {
sort session Subject Period TimeStamp

by session Subject Period: replace mess`i'=mess`i'+mess`i'[_n-1] if _n>1

local i=`i'+1
}




 
local i=1
while `i'<33 {
sort session Subject Period TimeStamp

by session Subject Period: gen F_mess`i'=mess`i' if _n==1
by session Subject Period: replace F_mess`i'=mess`i'[_n]+mess`i'[_n-1] if _n>1



local i=`i'+1
}

 
 local i=1
while `i'<33 {

replace message=2 if Subject==`i'&F_mess`i'>1


local i=`i'+1
}







 local j=1
while `j'<5 {

gen MessagesReceived`j'=0

local i=1
while `i'<33 {

sort session Subject Period TimeStamp 
replace MessagesReceived`j'=mess`i'[_n-1] if   LinkedSubjects`j'==`i'
replace MessagesReceived`j'=0 if TimeStamp==1

local i=`i'+1
 }
 
local j=`j'+1
 }
 

 
 
 gen MessRec= MessagesReceived1+MessagesReceived2+MessagesReceived3+MessagesReceived4
 
 replace MessRec=1 if MessRec>1
 

sort NetGroup Period TimeStamp Subject

by NetGroup Period TimeStamp: egen pippo2=total(MessRec)



*** To obtain the numbers and test for period=1, activate command "drop if Period>1" below  
*** To obtain the numbers and test for period<6, activate command "drop if Period>5" below  
*** To obtain the numbers and test for the reduced sample, activate command "drop if pippo2==8|pippo2==0" below  

preserve

drop if message>1

*drop if Period>1
*drop if Period>5
* drop if pippo2==8|pippo2==0

gen message_r_0=message==1&MessRec==0
gen message_r_1=message==1&MessRec==1

sort session MatchingGroup
by session MatchingGroup: gen num_obs=_N
by session MatchingGroup: egen num_obs_r_1=total(MessRec)
by session MatchingGroup: gen index2=_n==1
gen num_obs_r_0=num_obs-num_obs_r_1

sort session MatchingGroup
by session MatchingGroup:egen num_mess_r_0=total(message_r_0)
by session MatchingGroup:egen num_mess_r_1=total(message_r_1)

gen p_0=num_mess_r_0/num_obs_r_0
gen p_1=num_mess_r_1/num_obs_r_1

signrank p_0= p_1 if index2==1
signtest p_0= p_1 if index2==1

replace p_0=. if MessRec==1
replace p_1=. if MessRec==0

sum p_0 p_1

restore
